package com.dayu.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dayu.entity.Person;
import com.dayu.service.PersonService;

/**
 * @author dayu
 * @date 2018/10/29
 */
@Controller
@RequestMapping("hello")
public class HelloController {

    private static final Logger logger = Logger.getLogger(HelloController.class);

    @Resource
    private PersonService personService;

    @RequestMapping("say")
    public void sayHello(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("HelloController.sayHello()");
        String name = req.getParameter("name");
        logger.info("name=" + name);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        try {
            PrintWriter writer = resp.getWriter();
            writer.println("hello " + name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("HelloController.index()");
        return "index2";
    }

    @Test
    public void testQueryPerson() {
        String path = "application-context.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);
        
        HelloController testPerson = (HelloController)context.getBean("helloController");
        if(null !=testPerson) {
            Person person = testPerson.personService.queryPersonById(1);
            if (null != person) {
                System.out.println(person.toString());
            }            
            System.out.println("yes======");
        }else {
            System.out.println("nullllll");
        }

    }
}
